package com.fenghongzhang.day004;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.util.Log;
import android.widget.RadioGroup;

import java.util.Random;

public class Main2Activity extends AppCompatActivity {
    private RadioGroup group;

    private static final String TAG = "Main2Activity";
    private FragmentManager manager;
    private OneFragment oneFragment;
    private TwoFragment twoFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);


        Log.i(TAG, "onCreate: ");

        group = (RadioGroup) findViewById(R.id.group);








        manager = getSupportFragmentManager();
        final FragmentTransaction fragmentTransaction = manager.beginTransaction();
        oneFragment = new OneFragment();
        fragmentTransaction.add(R.id.ff,oneFragment);
        twoFragment = new TwoFragment();
        fragmentTransaction.add(R.id.ff,twoFragment);
        fragmentTransaction.hide(twoFragment);

        fragmentTransaction.commit(); //提交完成,需要重新开启一个事务.

        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.person:

                        FragmentTransaction fragmentTransaction1 = manager.beginTransaction();
                        fragmentTransaction1.hide(twoFragment);
                        fragmentTransaction1.show(oneFragment);
                        fragmentTransaction1.commit();
                        break;
                    case R.id.info:

                        FragmentTransaction fragmentTransaction2 = manager.beginTransaction();
                        fragmentTransaction2.hide(oneFragment);
                        fragmentTransaction2.show(twoFragment);
                        fragmentTransaction2.commit();
                        break;
                }
            }
        });
    }
}
